var fnColumnFilter = function( oSettings, oData ) {    
    //oSettings.headerCallback = headerCallback;
    this.settings()[0].aoHeaderCallback.push(headerCallback);
    //this.draw();
};
var headerInitArray = [];
var headerCallback = function(thead, data, start, end, display ) {
	var table = this;
    if (data.length > 0 ) {		    	
        $(thead).find('th').each(function(index, col){
            var theader = $(col);
            if (!headerInitArray[index] || headerInitArray[index] == false) {
            	headerInitArray.push(true);
            	theader.addClass("filter-header");
                var theaderTxt = theader.html();
                theader.empty();
                // 表头
                $("<span/>").appendTo(theader).html(theaderTxt);
                //$("<i/>", {class:"fa fa-filter pull-right"}).appendTo(theader);
                // 下拉选框
                var selectName = config.columns[index].data;
                //var selectEle = $("<select/>",{class:"form-control",name:"selector"}).appendTo(theader);
                //$("<option/>",{value:"", selected:"selected"}).appendTo(selectEle).html("--" + selectName + "--");
                // 过滤器
                var filterContainer = $("<div/>", {class:"pull-right th-tools"}).appendTo(theader);
                var filterGroup = $("<div/>", {class:"btn-group pull-right"}).appendTo(filterContainer);
                var filterBtn = $("<button/>", {class:"btn btn-box-tool dropdown-toggle", "data-toggle":"dropdown"}).appendTo(filterGroup);
                $("<i/>", {class:"fa fa-filter"}).appendTo(filterBtn);
//                $("<span/>", {class:"caret"}).appendTo(filterBtn);
                var drowDown = $("<ul/>", {class:"dropdown-menu", role:"menu"}).appendTo(filterGroup);
                
                // 下拉选中式样切换
                var filterEleFunc = function() {
                	$(this).toggleClass("selected");
                	//$("<i/>",{class:"fa fa-check"}).appendTo($("a", $(this)));
                	filterAction();
                }
                // 过滤动作执行
                var filterAction = function() {
                	var selectEle = [];
                	$("li", drowDown).each(function(){
                		if ($(this).hasClass("selected")) {
                			selectEle.push($("a", $(this)).html().replace("(","\(").replace(")","\)"));
                		}
                	});
//                	var regex = new RegExp("^" + selectEle.join("|") + "$");
                	var regex = new RegExp("^(" + selectEle.join("|") + ")$");
                	if (selectEle.length > 0) {
                		table.columns(index).search(regex.source ,true, false).draw();
                	} else {
                		table.columns(index).search("").draw();
                	}
                	
//                	var regex = new RegExp("^(" + selectEle.join("|") + ")$");
//                	var filterData = table.columns(index).data()[0].filter(function (value) {
//                        return regex.test(value);
//                    });
                }
                filterBtn.on("hide.bs.dropdown", filterAction);
                $(data).each(function(i,d){
                    //$("<option/>",{value:i}).appendTo(selectEle).html(d[selectName]);
                    var filterEle = $("<li/>").appendTo(drowDown).html('<a href="#">' +d[selectName] + '</a>');
                    filterEle.on("click", filterEleFunc);
                });
                $('#column3_search').on( 'keyup', function () {
                    table
                        .columns( 3 )
                        .search( this.value )
                        .draw();
                } );
//                selectEle.on("change", function(){
//                    if (this.value !== "") {
//                        riskTable.columns(index).search( this.selectedOptions[0].text ).draw();
//                    } else {
//                        riskTable.columns(index).search("").draw();
//                    }
//                })
            }

        })
    }
}
jQuery.fn.dataTable.Api.register( 'fnColumnFilter()', fnColumnFilter);
